<?php 
global $_GPC, $_W;
$shopid=check_right(3);
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
//if (!$_W['isfounder'] && $_W['role']!="manager")
if($operation=="display"){
    $list=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_ad')." WHERE weid='{$_W['uniacid']}' and groupid=0 order by displayorder asc,id desc ");

    $shoplist=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid='{$_W['uniacid']}' order by id desc ");
    if($shopid)$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid='{$_W['uniacid']}' and groupid=:a order by id desc ",array(":a"=>$shopid));

}elseif ($operation == 'getpcatetable') {
    $pcate=intval($_GPC['pcate']);
    if(!$pcate && !$_W['isfounder'] && $_W['role']!="manager")die(json_encode(array('success'=>false,'msg'=>"请选择区域")));
    $list=pdo_fetchall("SELECT * FROM " . tablename('j_hmoney_ad') . " WHERE weid = '{$_W['uniacid']}' and groupid=:a order by status desc,displayorder asc,id asc",array(":a"=>$pcate));
    for($i=0;$i<count($list);$i++){
        $temp=$list[$i]['thumb'];
        $list[$i]['thumb']=tomedia($temp);
    }
    die(json_encode(array('success'=>true,'list'=>$list)));

}elseif ($operation == 'gettable') {
    $id=intval($_GPC['id']);
    if(!$id)die(json_encode(array('success'=>false,'msg'=>"请选择要修改的内容")));
    $item=pdo_fetch("SELECT * FROM " . tablename('j_hmoney_ad') . " WHERE weid = '{$_W['uniacid']}' and id=:a ",array(":a"=>$id));
    $item['start']=date('Y-m-d',$item['starttime']);
    $item['end']=date('Y-m-d',$item['endtime']);
    $item['thumb1']=tomedia($item['thumb']);
    die(json_encode(array('success'=>true,'item'=>$item)));
}elseif ($operation == 'post') {
    $id=intval($_GPC['id']);
    $shoplist=pdo_fetchall("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid='{$_W['uniacid']}' order by id desc ");
    if($shopid)$shop=pdo_fetch("SELECT * FROM ".tablename('j_hmoney_group')." WHERE weid='{$_W['uniacid']}' and groupid=:a order by id desc ",array(":a"=>$shopid));
    if($id){
        $item=pdo_fetch("SELECT * FROM " . tablename('j_hmoney_ad') . " WHERE weid = '{$_W['uniacid']}' and id=:a ",array(":a"=>$id));
    }else{
        $item=array(
            "starttime"=>strtotime(date('Y-m-d')),
            "endtime"=>strtotime(date('Y-m-d')),
        );
    }
    if (checksubmit('submit')){
        if (empty($_GPC['title']))message("名称不能为空");
        $data = array(
            'weid' => $_W['uniacid'],
            'title' => $_GPC['title'],
            'groupid' => intval($_GPC['pcate']),
            'thumb' => trim($_GPC['thumb']),
            'info' => htmlspecialchars_decode($_GPC['info']),
            'starttime' => strtotime($_GPC['gametime']['start']),
            'endtime' => strtotime($_GPC['gametime']['end']),
            'status' => intval($_GPC["status"]),
        );
        if (!empty($id)) {
            pdo_update('j_hmoney_ad', $data, array('id' => $id));
        } else {
            $data["createtime"]=TIMESTAMP;
            pdo_insert('j_hmoney_ad', $data);
            $id = pdo_insertid();
        }
        message("更新完成",$this->createWebUrl("admanage"),'success');
    }
}elseif ($operation == 'posttable') {
    $id=intval($_GPC['id']);

    $item=pdo_fetch("SELECT * FROM " . tablename('j_hmoney_ad') . " WHERE weid = '{$_W['uniacid']}' and id=:a ",array(":a"=>$id));
    $item['start']=date('Y-m-d',$item['starttime']);
    $item['end']=date('Y-m-d',$item['endtime']);
    die(json_encode(array('success'=>true,'item'=>$item)));

}elseif ($operation == 'resettableorder') {
    $idary=explode(",",$_GPC['displayorder']);
    $sql="update ".tablename('j_hmoney_ad')." SET `displayorder` = CASE id ";
    for($i=0;$i<count($idary);$i++){
        $sql.=" WHEN ".$idary[$i]." THEN ".($i+1)." ";
    }
    $sql.=" END ";
    $sql.=" WHERE id IN (".$_GPC['displayorder'].") ";
    pdo_run($sql);
    die(json_encode(array('success'=>true)));
}elseif ($operation == 'deletetable') {
    $id=intval($_GPC['id']);
    if($id){
        $isUsed=pdo_fetch("SELECT * FROM " . tablename('j_hmoney_ad') . " WHERE weid = '{$_W['uniacid']}' and id=:a ",array(":a"=>$id));
        if($isUsed){
            pdo_delete('j_hmoney_ad',array('id'=>$id));
        }
    }
    message("删除成功", $this->createWebUrl('admanage'), 'success');
}
include $this->template('web/admanage');








